package org.clipjoint.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.clipjoint.dao.GameDao;
import org.clipjoint.entity.Game;
import org.clipjoint.entity.Purchased;
import org.clipjoint.util.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class GameDaoImpl implements GameDao {


    @Override
    public List<Game> RPGGame() throws SQLException {
        // 创建DButils核心对象
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        // 编写sql语句
        String sql = "SELECT * FROM `game` where `type` = '角色扮演' limit 4";

        // 执行sql
        List<Game> result = qr.query(sql, new BeanListHandler<>(Game.class));

        return result;

    }

    @Override
    public List<Game> BanGAME() throws SQLException {
        // 创建DButils核心对象
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        // 编写sql语句
        String sql = "SELECT * FROM `game` where `publisher_id` = 2 limit 4";

        // 执行sql
        List<Game> result = qr.query(sql, new BeanListHandler<>(Game.class));

        return result;
    }

    @Override
    public List<Game> showNew() throws SQLException {
        // 创建DButils核心对象
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        // 编写sql语句
        String sql = "SELECT * FROM `game` order by `time` desc limit 10";

        // 执行sql
        List<Game> result = qr.query(sql, new BeanListHandler<>(Game.class));

        return result;
    }

    @Override
    public List<Purchased> hot() throws SQLException {
        // 创建DButils核心对象
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        // 编写sql语句
        String sql = "SELECT COUNT(*),game_id,game_name,game_image,game_price FROM purchased GROUP BY game_id ORDER BY COUNT(*) DESC limit 10";

        // 执行sql
        List<Purchased> result = qr.query(sql, new BeanListHandler<>(Purchased.class));

        return result;
    }


    @Override
    public int delete(Long keyword) throws SQLException {
        // 创建DButils核心对象
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());

        // 编写sql语句
        String sql = "update `keyword` set `detail` = 1 where `keyword` = ?";

        int result = qr.update(sql,keyword);
        return result;
    }

}
